package com.dmwork.service.interaction.controller.admin;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dmwork.common.base.result.R;
import com.dmwork.service.interaction.entity.CmsCourse;
import com.dmwork.service.interaction.entity.vo.CmsCourseVo;
import com.dmwork.service.interaction.service.CmsCourseService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.LinkedList;
import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author 李天
 * @since 2021-12-13
 */
@RestController
@RequestMapping("/admin/dm_work/interaction/cms_course")
public class AdminCmsCourseController {

    @Autowired
    private CmsCourseService cmsCourseService;

    @Autowired
    private RedisTemplate redisTemplate;

    private static final String KEY = "course_list";

    @PostMapping("add_course")
    public R addCourse(@RequestBody CmsCourse cmsCourse) {
        boolean save = cmsCourseService.save(cmsCourse);
        if (save) {
            redisTemplate.opsForValue().set(KEY, cmsCourseService.getCourseList());
            return R.ok();
        }
        return R.error();
    }

    @PutMapping("modify_course")
    public R modifyCourse(@RequestBody CmsCourse cmsCourse) {
        boolean update = cmsCourseService.updateById(cmsCourse);
        if (update) {
            redisTemplate.opsForValue().set(KEY, cmsCourseService.getCourseList());
            return R.ok();
        }
        return R.error();
    }

    @DeleteMapping("remove_course/{id}")
    public R removeCourse(@PathVariable String id) {
        boolean remove = cmsCourseService.removeById(id);
        if (remove) {
            List<CmsCourseVo> courseList = cmsCourseService.getCourseList();
            if (courseList != null && courseList.size() > 0) {
                redisTemplate.opsForValue().set(KEY, cmsCourseService.getCourseList());
            } else {
                redisTemplate.delete(KEY);
            }
            return R.ok();
        }
        return R.error();
    }

    @GetMapping("get_course_list/{page}/{limit}")
    public R getCourseList(@PathVariable Integer page, @PathVariable Integer limit) {
        Page<CmsCourse> coursePage = cmsCourseService.getCourseList(page, limit);
        List<CmsCourseVo> cmsCourseVoList = new LinkedList<>();
        for (CmsCourse course : coursePage.getRecords()) {
            CmsCourseVo cmsCourseVo = new CmsCourseVo();
            BeanUtils.copyProperties(course, cmsCourseVo);
            cmsCourseVoList.add(cmsCourseVo);
        }
        return R.ok().data("total", coursePage.getTotal()).data("rows", cmsCourseVoList);
    }

    @GetMapping("get_course_by_id/{id}")
    public R<CmsCourseVo> getCourseById(@PathVariable String id) {
        CmsCourse cmsCourse = cmsCourseService.getById(id);
        CmsCourseVo courseVo = new CmsCourseVo();
        BeanUtils.copyProperties(cmsCourse, courseVo);
        return R.ok("item", courseVo);
    }


}

